<?php

	function FirstLoginCheckPressed() {
		session_start();
		include_once('../config.php'); 
		LinkDataBase();
		
		$sql = "SELECT * FROM member WHERE id = '".$_SESSION['ID']."'";
		$result= mysql_query($sql);
		$rowUser = mysql_fetch_assoc($result);
		
		if ($rowUser['firstlogin']==0)
			echo '<script> AlarmChangePassword(); </script>';
	}
	
	if ($_POST['Newpassword']) {
		session_start();
		echo $_POST['Newpassword'];
		echo 'ID:'.$_SESSION['ID'];
		include_once('../config.php'); 
		LinkDataBase();
		$sql = "UPDATE member set password = '".$_POST['Newpassword']."', firstlogin=1 WHERE id = '".$_SESSION['ID']."'";
		mysql_query($sql);
	}
?>
<style>

.ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only {
	float:left;
     text-align: center; /* left/center/right */
}
</style>
<script>
	
	
	function AlarmChangePassword() {
		
		console.log('Alarm');
		var password = $( "#password" ),
		allFields = $( [] ).add( password ),
		tips = $( ".validateTips" );
	 
		function updateTips( t ) {
		  tips
			.text( t )
			.addClass( "ui-state-highlight" );
		  setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		  }, 500 );
		}
	 
		function checkLength( o, n, min, max ) {
		  if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( "Length of " + n + " must be between " +
			  min + " and " + max + "." );
			return false;
		  } else {
			return true;
		  }
		}
	 
		function checkRegexp( o, regexp, n ) {
		  if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			updateTips( n );
			return false;
		  } else {
			return true;
		  }
		}
		
		$( "#dialog-form" ).dialog({
		  autoOpen: false,
		  position: ['top','right'],
		  height: 200,
		  width: 350,
		  modal: true,
		  buttons: {
			"Change password": function() {
				var bValid = true;
			  	bValid = bValid && checkLength( password, "password", 1, 16);
			  
			  	bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
	 
			  	if (bValid) {
					console.log(password.val());
					$.ajax({
						type: 'POST',
						url: "../detail/firstlogin.php",
						data: {Newpassword: password.val()},
						success: function(data){
							console.log(data);
							$("#dialog-form").dialog("close");
						}
					});
			  	}
			}
		  }
		});
		
    	$("#dialog-form").parent().children().children('.ui-dialog-titlebar-close').hide();
        $("#dialog-form").dialog("open");
	}
</script>
<!----- Dialog Confirm ------>
<div id="dialog-form" title="Change password" hidden="YES">
  <p class="validateTips"></p>
  <form>
  <fieldset>
    <label for="password">Password</label>
    <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
  </fieldset>
  </form>
</div>